package cn.yitun.chat.yitunservermanagecore.general.manage.chart.service.impl;

import cn.yitun.chat.yitunservercommonlib.dataentity.dto.chart.ChartBodyDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.chart.ChartBodyVo;
import cn.yitun.chat.yitunservercommonlib.util.CalendarAdjust;
import cn.yitun.chat.yitunserverimbase.dao.common.LogDao;
import cn.yitun.chat.yitunserverimbase.dao.login.LoginLogDao;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunservermanagecore.general.manage.chart.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 图表数据
 * @author chenyitun
 * @date 2020-06-13
 */
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;
    @Autowired
    private LogDao logDao;
    /**
     * 线状图
     * @return
     */
    @Override
    public Map<String, Object> lineChartData(ChartBodyDto chartBobyDto) {
        Map<String , Object> results = new HashMap<>();
        chartBobyDto.setBegin(CalendarAdjust.getSomedayStartTime(new Date().getTime(), "GMT+8:00",chartBobyDto.getColumns() , Calendar.DATE));
        chartBobyDto.setEnd(CalendarAdjust.getDailyStartTime(new Date().getTime(), "GMT+8:00"));
        results.put("titleText" ,"用户数其操作统计" );
        List<String> dates = listAvgDate(chartBobyDto.getColumns() , Calendar.DATE);
        results.put("dateList" ,  dates);
        List<ChartBodyVo> bodyVoList = userDao.countAvgDate(chartBobyDto);
        int baseCount = userDao.baseCountChart(chartBobyDto);
        results.put("userList" ,dealBodysDefaultZero(bodyVoList , dates , baseCount) );
        bodyVoList = loginLogDao.countAvgDate(chartBobyDto);
        results.put("loginLogList" ,dealBodysDefaultZero(bodyVoList , dates ));
        bodyVoList = logDao.countAvgDate(chartBobyDto);
        results.put("logList" ,dealBodysDefaultZero(bodyVoList , dates ) );
        return results;
    }

    /**
     * 根据列数和间隔获取时间数组
     * @param columns
     * @param timeType 时间类型，与Calendar.DATE type对应
     * @return
     */
    public static List<String> listAvgDate(int columns, int timeType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dates = new ArrayList<>();
        for (int i = 0 ; i < columns ; i ++){
            calendar.add(timeType, -1);
            dates.add(0 ,simpleDateFormat.format(calendar.getTime()));
        }
        return dates;
    }

    /**
     *
     * @param bodyVoList
     * @param dates
     * @return
     */
    private static List<Integer> dealBodysDefaultZero(List<ChartBodyVo> bodyVoList, List<String> dates ) {
        List<Integer> counts = new ArrayList<>();
        for ( String date : dates){
//            System.out.println(date );
            int count = 0;
            for (ChartBodyVo bodyVo : bodyVoList){
                if (bodyVo.getCountDay().equals(date)){
                    count = bodyVo.getCountSum();
                    break;
                }
            }
            counts.add(count);
        }
        return counts;
    }
    /**
     *
     * @param bodyVoList
     * @param dates
     * @param baseCount 基数
     * @return
     */
    private static List<Integer> dealBodysDefaultZero(List<ChartBodyVo> bodyVoList, List<String> dates ,int baseCount) {
        List<Integer> counts = new ArrayList<>();
        for ( String date : dates){
//            System.out.println(date );
            int count = 0;
            for (ChartBodyVo bodyVo : bodyVoList){
                if (bodyVo.getCountDay().equals(date)){
                    count = bodyVo.getCountSum();
                    break;
                }
            }
            baseCount += count;
            counts.add(baseCount);
        }
        return counts;
    }
}
